package com.xiay.stringboot.ali;

import com.alibaba.fastjson.JSONObject;
import com.antgroup.zmxy.openplatform.api.DefaultZhimaClient;
import com.antgroup.zmxy.openplatform.api.ZhimaApiException;
import com.antgroup.zmxy.openplatform.api.request.ZhimaCustomerCertificationCertifyRequest;
import com.antgroup.zmxy.openplatform.api.request.ZhimaCustomerCertificationInitializeRequest;
import com.antgroup.zmxy.openplatform.api.request.ZhimaCustomerCertificationQueryRequest;
import com.antgroup.zmxy.openplatform.api.response.ZhimaCustomerCertificationInitializeResponse;
import com.antgroup.zmxy.openplatform.api.response.ZhimaCustomerCertificationQueryResponse;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by H-209-FX-XY on 2018/5/8.
 */
public class ZhimaCustomerCertification {
    //芝麻开放平台地址
    private static final String gatewayUrl     = "https://zmopenapi.zmxy.com.cn/sandbox.do";
    //商户应用 Id
    private static final String appId          = "300002680";
    //商户 RSA 私钥
    private static final String privateKey     = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIjfNWUXqCNtlaBFpv4KrBTVQb4btX9Gi+UPuz7AMvwbSXmtIxV2awkz4Rg0Q3PtTkuAUpuW958u3uNxypcsCJD3o4qphgqSMVpe33bJ1bZaeL/h8WT9KNBiIxGsYQco44JbJkjrHTAI3Wt+RNhHyn+nKI9wCaeYPCY7ZipVpmL3AgMBAAECgYBn/BxWx1hIQjMQ5pnuGzGNSk9+HRMQtQoHZqI9FEwn2JtDw9QJtEOxZCa4+svcQQfguIcKCfHqj/NqHMNrglqmmJhKrut+ilUt8rqJCmvRSh8AdG7ZnqzxQYdTrrEEKgO932WqPOI98X2H+2cPzLfIOgxN7FhCDg+lVi4CBdfJ8QJBANosNf2b4f50AgAkstAKX1Hqqsb+k5L5reoF9e6eQufPXIpSrbME3cETyRMMLKX+YfhHpv66fpfg0X4JiHZIpdsCQQCgmmPNWs2nbvHY3Eo3xuYs/KJRIltgYaAd9+CCjpCTbU21afqyGkK7zibHfLADh+m5GOkOZ5IFn5Alhg1NQNgVAkBTX/PeEDVEPWcKUPv4nw4gSvKqi10wHLSGq3J5lwdweQEfZ0s0D5cDEyGTYuKpKNadwBwkWnbIacUFSnVY5phjAkAt4Ix73+F5X77kROFKl52u4if350mU+a5EgUd35AO2qXWWSgTcFZZUkaoQODULfSqtvkjs3Xcf9hm2LlnkZI6VAkEAl8+0+a6Rjz119mxJXy9JWKTbWirhja+ijyndLVEAmxTYjWTuNKbRdTYe59ekRYhmMWkiKzggP+PKjXHCHV6eRQ==";
    //芝麻 RSA 公钥
    private static final String zhimaPublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9Oa78HWtmz8p1QINVzt9RPKTbs6LdV6kaNZ2XWnoTbJZWA8eHYlRYq+uaKamHZrDX3stA2fiBSEHGekiULN2snvM+vo418qbcUoB/cd62nYxSQUeVMXVOD6PEOlrsm1vZAhPjEt2hns6F78K6CfbiUxs5b93P4gVk3YbF+5CJaQIDAQAB";

    private static DefaultZhimaClient client = new DefaultZhimaClient(gatewayUrl, appId, privateKey, zhimaPublicKey);

    private static String  initialize() {
        ZhimaCustomerCertificationInitializeRequest req = new ZhimaCustomerCertificationInitializeRequest();
        req.setChannel("apppc");
        req.setPlatform("zmop");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String tid = "JXFX"+ sdf.format(new Date()) + "00012341111111";
        System.out.println(tid);
        req.setTransactionId(tid);// 必要参数
        req.setProductCode("w1010100000000002978");// 必要参数
        req.setBizCode("FACE");// 必要参数
        // cert_no = 42010319900411371X
        req.setIdentityParam("{\"identity_type\": \"CERT_INFO\", \"cert_type\": \"IDENTITY_CARD\", \"cert_name\": \"田代勇\", \"cert_no\":\"61230119820108279X\"}");// 必要参数
        req.setMerchantConfig("{\"need_user_authorization\":\"false\"}");//
        req.setExtBizParam("{}");// 必要参数
        try {
            ZhimaCustomerCertificationInitializeResponse response = client.execute(req);
            System.out.println(response.getBody().toString());
            if(response.isSuccess()){
                JSONObject jsonObject = JSONObject.parseObject(response.getBody());
               return jsonObject.getString("biz_no");
            }
        } catch (ZhimaApiException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void  certify() {
        String biz_no = initialize();
        if(biz_no == null || biz_no.equals("")){
            System.out.println("initialize 失败");
            return ;
        }
        ZhimaCustomerCertificationCertifyRequest req = new ZhimaCustomerCertificationCertifyRequest();
        req.setChannel("apppc");
        req.setPlatform("zmop");
        req.setBizNo(biz_no);// 必要参数
        req.setReturnUrl("http://www.taobao.com");// 必要参数
        try {
            String url = client.generatePageRedirectInvokeUrl(req);
            System.out.println(url);
        } catch (ZhimaApiException e) {
            e.printStackTrace();
        }
    }

    public static void query(String biz_no){
        ZhimaCustomerCertificationQueryRequest req = new ZhimaCustomerCertificationQueryRequest();
        req.setChannel("apppc");
        req.setPlatform("zmop");
        req.setBizNo(biz_no);// 必要参数
        DefaultZhimaClient client = new DefaultZhimaClient(gatewayUrl, appId, privateKey, zhimaPublicKey);
        try {
            ZhimaCustomerCertificationQueryResponse response =(ZhimaCustomerCertificationQueryResponse)client.execute(req);
            System.out.println(response.getBody());

        } catch (ZhimaApiException e) {
            e.printStackTrace();
        }
    }
}
